package com.example.yan_music.base.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.WindowManager;

import com.example.yan_music.R;
import com.example.yan_music.base.view.IView;
import com.example.yan_music.utils.DipAndPxUtils;
import com.example.yan_music.utils.XToast;
import com.example.yan_music.utils.statusbar.StatusBarUtil;
import com.orhanobut.logger.Logger;

public class BaseActivity extends AppCompatActivity implements IView {

    private static final int REQUEST_EXTERNAL_STORAGE = 1;
    private static String[] PERMISSIONS_STORAGE = {
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 修改状态栏的背景色，修改状态栏文字为深色
        modifyStatusBar(R.color.statusBarColor_main_white);
        setContentView(R.layout.activity_base);
        verifyStoragePermissions(this);
        // 获取设备宽高，并打印
        getDeviceSizeFroDp();
    }

    @Override
    public void showLoadingDialog() {

    }

    @Override
    public void dismissLoadingDialog() {

    }

    @Override
    public void showToast(String msg) {
        XToast.show(msg, XToast.Type.POINT);
    }

    @Override
    public void showSingleDialog(int dialogRes) {

    }


    /**
     * 添加 Fragment
     *
     * @param container_id
     * @param fragment
     */
    public void addFragment(int container_id, Fragment fragment) {
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.add(container_id, fragment);
//        transaction.addToBackStack(null);
        transaction.commit();
    }

    /**
     * 获取设备宽高，并打印log
     */
    public void getDeviceSizeFroDp() {
        WindowManager wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        int width = dm.widthPixels; // 屏幕宽度（像素）
        int height = dm.heightPixels; // 屏幕高度（像素）
        float density = dm.density; // 屏幕的密度，用来计算dp
        int screenWidth = (int) (width / density); // 屏幕的宽度（dp）
        int screenHeight = (int) (height / density); // 屏幕的高度（dp）
        Logger.d("该设备的 宽度 为：[ " + screenWidth + " ]" + "dp");
        Logger.d("该设备的 高度 为：[ " + screenHeight + " ]" + "dp");
    }

    /**
     * 获取状态栏高度
     *
     * @return
     */
    public int getStatusBarHeight(){
        int height = this.getResources().getDimensionPixelSize(this.getResources().getIdentifier("status_bar_height", "dimen", "android"));
        return height;
    }

    /**
     * 获取导航栏高度
     *
     * @return
     */
    public int getNavigationBarHeight(){
        int height = this.getResources().getDimensionPixelSize(this.getResources().getIdentifier("navigation_bar_height", "dimen", "android"));
        return height;
    }

    /**
     * 只修改修改状态栏颜色，不改变文字图标颜色
     *
     * @param statusBarColor 状态栏颜色
     */
    public void modifyStatusBar(int statusBarColor) {
        StatusBarUtil.setStatusBarMode(this, false, statusBarColor);
    }

    /**
     * 同时修改状态栏颜色和文字图标颜色
     *
     * @param isTextDark
     * @param statusBarColor
     */
    public void modifyStatusBar(boolean isTextDark, int statusBarColor) {
        StatusBarUtil.setStatusBarMode(this, true, statusBarColor);
    }

    /**
     * 修改底部导航栏颜色
     *
     * @param navigationBarColor
     */
    public void modifyNavigationBar(int navigationBarColor) {
        getWindow().setNavigationBarColor(getColor(navigationBarColor));
    }

    /**
     * 申请权限
     *
     * @param activity
     */
    public static void verifyStoragePermissions(Activity activity) {
        // Check if we have write permission
        int permission = ActivityCompat.checkSelfPermission(activity,
                Manifest.permission.WRITE_EXTERNAL_STORAGE);

        if (permission != PackageManager.PERMISSION_GRANTED) {
            // We don't have permission so prompt the user
            ActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE,
                    REQUEST_EXTERNAL_STORAGE);
        }
    }
}